package com.controller;

import java.net.BindException;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.EmailInfo;
import com.constant.Constant;
import com.qufenqi.pay.abs.common.exception.BusinessException;
import com.qufenqi.pay.abs.common.utils.BaseParamValidate;
import com.service.SendService;
import com.service.impl.SendServiceImpl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.utils.Check;
import com.utils.Utils;

@Controller
@RequestMapping("/mail")
public class Mail {
	SendService iSendService = new SendServiceImpl();

	@RequestMapping(value = "/send", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String sender(EmailInfo mail, HttpServletRequest req) { // Model

		JSONObject json = new JSONObject();
		Utils utils = new Utils();

		try {
			BaseParamValidate.validate(mail);
			Check.check(mail);
			// utils.send(mail.getTitle(), mail.getContext());
			iSendService.send(mail.getTitle(), mail.getContext());
			System.out.println(mail.toString());
		} catch (BusinessException biz) {
			biz.getMessage();
			json.put("code", biz.getErrorCode());
			json.put("msg", biz.getMessage());
			return json.toString();
		} catch (Exception e) {
			json.put("code", "SYS001");
			json.put("msg", e.getMessage());
			e.printStackTrace();
			return json.toString();
		}
		json.put("code", "SUCCESS");
		json.put("msg", "发送成功");
		return json.toString();
	}

	@RequestMapping(value = "/sendWithCc", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String sendWithCc(EmailInfo mail, HttpServletRequest req) { // Model

		JSONObject json = new JSONObject();
		Utils utils = new Utils();

		try {
			BaseParamValidate.validate(mail);
			Check.check(mail);
			// utils.send(mail.getTitle(), mail.getContext());
			iSendService.sendWithCc(mail.getTitle(), mail.getContext(), mail
					.getCc());
			System.out.println(mail.toString());
		} catch (BusinessException biz) {
			biz.getMessage();
			json.put("code", biz.getErrorCode());
			json.put("msg", biz.getMessage());
			return json.toString();
		} catch (Exception e) {
			json.put("code", "SYS001");
			json.put("msg", e.getMessage());
			e.printStackTrace();
			return json.toString();
		}
		json.put("code", "SUCCESS");
		json.put("msg", "发送成功");
		return json.toString();
	}

	@RequestMapping(value = "/main", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String main(EmailInfo maill, HttpServletRequest req) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("msg", "这是主页面");
		jsonObject.put("code", "hello");
		return jsonObject.toString();
	}
}
